package com.bailan.foreat.service.impl;

import com.bailan.foreat.entity.Address;
import com.bailan.foreat.entity.Store;
import com.bailan.foreat.entity.StoreType;
import com.bailan.foreat.es.ESDataWriter;
import com.bailan.foreat.es.ESStoreRepository;
import com.bailan.foreat.mapper.AddressMapper;
import com.bailan.foreat.mapper.StoreMapper;
import com.bailan.foreat.mapper.StoreTypeMapper;
import com.bailan.foreat.service.StoreService;
import com.bailan.foreat.vo.EsStore;
import com.bailan.foreat.vo.StoreVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.elasticsearch.index.query.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ste
 * @since 2023-02-21 07:50:43
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {


    @Autowired
    ESStoreRepository esStoreRepository;

    @Autowired
    StoreMapper storeMapper;

    @Autowired
    StoreTypeMapper storeTypeMapper;

    @Autowired
    AddressMapper addressMapper;

    @Autowired
    @Qualifier("esStore")
    ESDataWriter esDataWriter;

    @Override
    public Page<EsStore> searchByES(Integer currentPage, Integer pageSize, String keyName, Integer keyType) {
        //判断es是否有数据,如果没有数据就写入
        if (!esStoreRepository.findAll().iterator().hasNext())
            esDataWriter.dataWrite();
        //es查询
        //构建查询条件
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();

        WildcardQueryBuilder query1 = null;
        WildcardQueryBuilder query2 = null;
        //query
        if (!"".equals(keyName) && keyName != null) {
            query1 = QueryBuilders.wildcardQuery("storeName", keyName);
        }

        if (keyType != 0) {
            StoreType storeType = storeTypeMapper.selectById(keyType);
            query2 = QueryBuilders.wildcardQuery("typeName", storeType.getTypeName());

        }
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (query1 != null)
            boolQueryBuilder.must(query1);
        if (query2 != null)
            boolQueryBuilder.must(query2);

        nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
        //分页条件
        //注意: es中的分页是从第0页开始的

        nativeSearchQueryBuilder.withPageable(PageRequest.of(currentPage - 1, pageSize));

        //排序
//        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("buyCount").order(SortOrder.DESC));

        org.springframework.data.domain.Page<EsStore> esStorePage = esStoreRepository.search(nativeSearchQueryBuilder.build());

        //将 Es的page 转换 mybatisPlus的 page
        Page<EsStore> storePage = new Page<>(currentPage, pageSize);
        storePage.setPages(esStorePage.getTotalPages());
        storePage.setTotal(esStorePage.getTotalElements());
        storePage.setRecords(esStorePage.getContent());

        return storePage;
    }

    @Override
    public Page selectAll(Integer currentPage, Integer pageSize) {
        Page page = new Page<>(currentPage, pageSize);
        Page<StoreVo> storeVoList = storeMapper.selectAllPage(page);
        return storeVoList;
    }

    @Override
    public Page selectByTypeId(Integer currentPage, Integer pageSize, Integer keyType) {
        Page page = new Page<>(currentPage, pageSize);
        StoreType storeType = storeTypeMapper.selectById(keyType);
        Page<StoreVo> storeVoList = storeMapper.selectByType(page, storeType.getTypeName());
        return storeVoList;

    }
    public StoreVo queryDetails(Integer storeId) {
        StoreVo storeVo=new StoreVo();
        Store store = storeMapper.selectById(storeId);
        storeVo.setId(store.getId());
        storeVo.setStoreName(store.getStoreName());
        storeVo.setStoreImg(store.getStoreImg());
        storeVo.setDetail(store.getDetail());
        storeVo.setAddressId(store.getAddressId());
        storeVo.setSellerId(store.getSellerId());
        storeVo.setStatus(store.getStatus());
        Address address = addressMapper.selectById(store.getAddressId());
        storeVo.setAddress(address.getProvince() + address.getCity() + address.getArea() + address.getDetailAddress());
        return storeVo;
    }
}
